package com.class06;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1、获取session
        HttpSession session = req.getSession();
//        2、获取sessionId
        String id = session.getId();

        System.out.println(id);
//        3、判断是否是新的session
        boolean aNew = session.isNew();
        System.out.println(aNew);
//        4、获取session中获取username的属性值
        String username = (String) session.getAttribute("username");
        //        设置输出类型和编码格式
        resp.setContentType("text/html;charset=utf-8");
//        设置响应编码格式
        resp.setCharacterEncoding("utf-8");
        if (username!=null) {
            resp.getWriter().write(username);
        } else {
            resp.getWriter().write("session中没有username");
        }


    }
}
